
Free Online Regex Tester
Try regular expressions against your text with live highlighting, group details, replace previews and a timeout that stops catastrophic patterns.
A regular expression is a compact pattern for finding and changing text. It is powerful, easy to get subtly wrong and hard to read a week later. This tester lets you write a pattern, paste some sample text and see every match highlighted at once, with details of each capture group. It also protects you from one classic hazard: patterns that run for ages on certain input. Everything stays in your browser.
How to use it
- Type your pattern in the top box, without the surrounding slashes.
- Switch flags on or off with the small buttons below it.
- Paste or type the text you want to search.
- Read the highlighted result and the table of matches, with line, column and group values.
- Open the Replace tab to preview a substitution using $1, $2 and $&.
The flags
| Flag | Meaning |
|---|---|
| g | Global: find every match, not just the first |
| i | Ignore case |
| m | Multiline: ^ and $ match at each line |
| s | Dot also matches new lines |
| u | Unicode mode for characters beyond the basic set |
| y | Sticky: match only at the current position |
A worked example
Take the pattern (\w+)@(\w+)\.com and the text “Contact ann@example.com or bob@sample.com for details.” Two matches are highlighted, and the table lists group 1 as ann and bob and group 2 as example and sample. In the Replace tab, the replacement $2: $1 turns the text into “Contact example: ann or sample: bob for details.” Seeing the groups numbered makes it much easier to write the replacement correctly.
Protection against slow patterns
Some patterns, such as (a+)+$, can take an enormous amount of time on text that almost matches. This is called catastrophic backtracking, and it can freeze a page or even be used to attack a website. This tester runs the pattern in a background worker with a limit of one and a half seconds. If the limit is reached, the worker is stopped and you see a message, so the page stays responsive. When you see it, simplify the pattern: avoid nested repeats and be more specific about what each part may match.
Common patterns
The Common patterns tab loads ready examples: whole and decimal numbers, an email-shaped text, web addresses, hex colours, ISO dates, repeated words, extra spaces, trimming line ends and capital-letter words. Each has a note on what it does and does not do. The email and web address patterns find text shaped like those things, but they are not validators, because real addresses follow rules that are too complex for a short pattern.
Debugging tips
- Start with the smallest pattern that matches something and add one piece at a time.
- Escape special characters such as . ( ) [ ] { } + * ? ^ $ | and the backslash when you mean them literally.
- Use non-greedy quantifiers such as *? when a match grabs too much.
- Anchor patterns with ^ and $ when validating a whole value.
- Test with text that should not match, not only text that should.
Limits
This tool uses the regular expression engine of your browser, which follows JavaScript rules. Python, PHP, Java, Perl and others share most basics but differ in details, such as named group syntax, lookbehind support and flags, so check your target language. Text is limited to 100,000 characters and results to 2,000 matches for speed. The page shows what a pattern does; it cannot tell you whether it is the right pattern for your purpose, so keep test cases.
For related text jobs, try find and replace, the text case converter or the URL encoder and decoder. The guide to choosing between Python and JavaScript helps you decide where to use patterns, and the JavaScript course shows how to run them in code.
Reading a pattern aloud
Another good habit is to read a pattern out loud, piece by piece. The pattern \d{4}-\d{2}-\d{2} says four digits, a dash, two digits, a dash, two digits. Once you can say it, you can check it. Add a short comment next to every pattern you keep in code, with one sample that should match and one that should not. Future you, or a colleague, will be grateful when the pattern needs changing.
Finally, keep your tests with your code. A pattern that passes ten sample strings today can quietly break when the input format changes next year, and a small list of examples that should and should not match lets you notice at once. Store the samples in the same file as the pattern, in a comment or a test, and run them whenever you change anything.
Key takeaways
- See all matches, groups and positions highlighted as you type.
- Preview replacements using $1, $2 and $& before using them in code.
- A one and a half second timeout stops catastrophic patterns safely.
- Patterns that look like emails or links are guides, not validators.
- Rules differ between languages, so verify in your own environment.
Questions
What does a regular expression do?
A regular expression describes a pattern of text, so a program can find, check or replace it. For example, \d+ finds runs of digits and ^\w+@ finds text at the start that ends with an at sign. Regular expressions are used in editors, spreadsheets, websites and many programming languages.
Why does my pattern freeze or time out?
Patterns with nested repeats, such as (a+)+$, can try an enormous number of ways to match when the text almost fits. This is called catastrophic backtracking. The tester stops any pattern after one and a half seconds. Rewrite it with simpler, more specific parts to make it fast.
What is the difference between greedy and lazy matching?
Greedy quantifiers such as * and + match as much text as they can, then give back if needed. Lazy versions such as *? and +? match as little as possible. For example, on the text a1b2b, the pattern a.*b takes everything up to the last b, while a.*?b stops at the first b.
Does this work for Python or PHP patterns?
It uses your browser, so it follows JavaScript rules. Basic syntax is shared with other languages, but differences exist in named groups, lookbehind, flags and Unicode handling. Use this tool to learn and sketch a pattern, then confirm it in the language where it will run, with real test data.
Can I validate an email address with a regex?
A short pattern can check that text looks like an email, but it cannot fully validate one, because the rules are complex and the only reliable test is sending a message. Use a simple pattern for a friendly format check and rely on confirmation emails for certainty.
